<template>

  <el-dialog
    :title="title"
    :visible.sync="show"
    width="46%"
    @close="close"
  >
    <el-form ref="form" :model="menu" :rules="rules" class="form" label-position="right" label-width="80px">
      <el-form-item label="上级菜单" prop="parentId">
        <select-tree ref="selectTree" :parent-id.sync="menu.parentId" :tree="tree" />
      </el-form-item>
      <el-row gutter="20">
        <el-col span="12">
          <el-form-item label="菜单编号" prop="menuCode">
            <el-input v-model="menu.menuCode" />
          </el-form-item>
        </el-col>
        <el-col span="12">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input v-model="menu.menuName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row gutter="20">
        <el-col span="12">
          <el-form-item label="菜单类型" prop="type">
            <el-radio v-model="menu.type" :label="0">目录</el-radio>
            <el-radio v-model="menu.type" :label="1">菜单</el-radio>
            <el-radio v-model="menu.type" :label="2">按钮</el-radio>
          </el-form-item>
        </el-col>
        <el-col span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="menu.sort" controls-position="right" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="menu.type !==2" gutter="20">
        <el-col span="12">
          <el-form-item label="图标" prop="icon">
            <el-select v-model="menu.icon" clearable filterable placeholder="请选择图标">
              <el-option
                v-for="item in svgIcons"
                :key="item"
                :label="item"
                :value="item"
              >
                <el-row>
                  <svg-icon :icon-class="item" /><span class="icon-label">{{ item }}</span>
                </el-row>
              </el-option>
            </el-select></el-form-item>
        </el-col>
        <el-col span="12">
          <el-form-item label="路由地址" prop="url" :rules="urlRule">
            <el-input v-model="menu.url" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item v-if="menu.type===1" label="组件路径" prop="component">
        <el-input v-model="menu.component" />
      </el-form-item>

      <el-form-item v-if="menu.type!==0" label="权限编码" prop="permissionCode">
        <el-input v-model="menu.permissionCode" />
      </el-form-item>

      <el-row v-if="menu.type !==2" gutter="20">
        <el-col span="12">
          <el-form-item label="是否外链" prop="externalLink">
            <el-radio v-model="menu.externalLink" :label="false">否</el-radio>
            <el-radio v-model="menu.externalLink" :label="true">是</el-radio>
          </el-form-item>
        </el-col>
        <el-col span="12">
          <el-form-item label="显示状态" prop="showed">
            <el-radio v-model="menu.showed" :label="false">否</el-radio>
            <el-radio v-model="menu.showed" :label="true">是</el-radio>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="菜单状态" prop="state">
        <el-radio v-model="menu.state" :label="false">否</el-radio>
        <el-radio v-model="menu.state" :label="true">是</el-radio>
      </el-form-item>

      <el-form-item label="备注" prop="note">
        <el-input v-model="menu.note" type="textarea" placeholder="请选择" :rows="2" />
      </el-form-item>
      <el-row class="form-button">
        <el-button type="primary" @click="save">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { find, save, tree, update } from '@/api/sys/menu'
import svgIcons from '@/views/icons/svg-icons'
import SelectTree from '@/components/SelectTree/index'

export default {
  name: 'MenuForm',
  components: { SelectTree },
  props: {
    id: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      default: '添加字典'
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      menu: { parentId: null, menuCode: '', menuName: '', type: 0, state: true, note: '', sort: 0,
        permissionCode: '', showed: true, externalLink: false, component: '', icon: '', url: '' },
      options: [{ label: '停用', value: 0 }, { label: '正常', value: 1 }],
      rules: {
        menuCode: [
          { required: true, message: '请输入岗位编号', trigger: 'blur' }
        ],
        menuName: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序值', trigger: 'blur' }
        ]
      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      svgIcons,
      filterText: '',
      urlRule: [
        { required: true, message: '请输入路由地址', trigger: 'blur' }
      ],
      tree: []
    }
  },
  watch: {
    id(val) {
      if (val) { this.find() }
    },
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.findTree()
  },
  methods: {
    close() {
      this.$refs.selectTree.$refs.tree.setCurrentKey(null)
      this.$refs['form'].resetFields()
      this.menu = { parentId: null, menuCode: '', menuName: '', type: 0, state: true, note: '', sort: 0,
        permissionCode: '', showed: true, externalLink: false, component: '', icon: '', url: '' }
      this.$emit('update:show', false)
      this.$emit('update:id', null)
    },
    save() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          const menu = { ...this.menu }
          if (menu.type === 0) {
            delete menu.permissionCode
          }
          if (menu.type === 2) {
            delete menu.icon
            delete menu.url
            delete menu.showed
            delete menu.externalLink
          }
          if (this.id) {
            await update(menu)
            this.responseRes()
          } else {
            await save(menu)
            this.responseRes()
          }
        }
      })
    },
    async find() {
      const { data } = await find(this.id)
      this.menu = data
    },

    responseRes() {
      this.close()
      this.$parent.findPage()
      this.findTree()
    },

    async findTree() {
      const { data } = await tree()
      this.tree = data
    }
  }
}
</script>

<style scoped>
.icon-label{
  margin-left: 1vh;
}
.el-input-number--medium{
 width: 100%;
}

.select-tree{
  padding: 0 0;
}
.selectInput{
  padding: 10px;
}
</style>
